#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""

@author ：吾非同
@date ：2024/4/21 下午1:37
"""
import time

money = 10
money = money - 1
print(money)
print(type(money))
print(bool(money))
print(bool(0))
if money:
    print("有钱")
else:
    print("没钱")

# 字符串
print("hello world")
print("1 + 1 =", 1 + 1)
#
print("1 + 1 = %d" % (1 + 1))
# 乘
print("1 * 2 =", 1 * 2)
# 除
print("4 / 2 = ", 4 / 2)
#  减
print("4 - 2 = ", 4 - 2)
# 取整
print("4 // 2 = ", 4 // 2)
# 取余
print("4 % 2 = ", 4 % 2)
# 幂
print("4 ** 2 = ", 4 ** 2)
#简写
num = 1
num += 1
print("num +=",num)
num -= 1
print("num -=",num)
num *= 1
print("num *=",num)
num = 4
num /= 2
print("num /=",num)
num = 5
num %= 3
print("num %=",num)
num = 5
num //= 2
print("num //=",num)
num = 2
num **= 2
print("num **=",num)
# 列表
list = [1, 2, 3, 4, 5]
print(list)
print(list[0])
print(list[1])
print(list[2])
print(list[3])
print(list[4])
print(list[-1])
print(list[-2])
print(list[-3])
print(list[-4])
print(list[-5])
print(list[:]) # 列表切片 不写则默认为0从头开始到结尾
# 列表切片从索引0开始到索引2（不包括索引2）的元素
print(list[0:2])
print(list[1:3])
print(list[2:4])
print(list[3:5])
print(list[4:6])
print("list[0:6] = ",list[0:6])
print(list[0:6:2])
# 倒序 反向输出，由于索引是从0开始，所以索引5是倒数第一个元素，因为end 不包含所以只有这么写，start会包含本身
print(f"{list} 倒序输出 = {list[5::-1]}")
print(f"{list} 倒序输出 = {list[::-1][0:6]}")#这种写法更好理解
print(list[0:6:3])
print(list[0:6:4])
print(list[0:6:5])
print(list[0:6:6])
print(list[0:6:7])
print(list[0:6:8])
print(list[0:6:9])
print(list[0:6:10])
print()
# 循环
num = 1
while num < 10:
    print(num)
    num += 1
print(num)
print("拼接字符串 %s  ===== %s",("111111","2222222"))

num1 = 11
num2 = 11.345
# 格式化输出
print("num1 = %4d,num2 = %.2f" % (num1,num2))
# 循环
for i in range(1,10):
    print(i)
# 步长
for i in range(1,10,2):
    print(i)
for i in range(10):
    print(i)
my_set = {1,2,3,4,5,6,7,8,9,10}
my_set.add(11)
my_set.remove(1)
# 随机删除一个元素，并返回被删除的元素
print(f"被删除的元素 = {my_set.pop()}，集合 = {my_set}")
my_set.clear()
for i in my_set:
    print(i)
set1 = {1,2,3}
set2 = {1,5,6}
print(f"取出set1中不在set2中的元素 ={set1.difference(set2)} set1 = {set1}")
print(f"并集 ={set1.union(set2)}")
print(f"set1添加元素 = {set1.add(4)}")
print(f"set1添加元素 = {set1.remove(4)}")
print(f"删除set1中与set2中重复的元素 ={set1.difference_update(set2)} set1 = {set1}")
for i, e in enumerate(set1):
    print(f"i= {i} , e = {e}")
for e in set1:
    print(f"e = {e}")
# 文件读写
file = open("test.txt", "r", encoding="UTF-8")
print(f"读取到内容1：{file.read(2)}")
print(f"读取到内容2：{file.read()}")
file.close()
file = open("test.txt", "r", encoding="UTF-8")
print(f"读取到内容3：{file.readline()}")
print(f"读取到内容4：{file.readlines()}")
# 清空文件（文件不存在创建），内容覆盖
file = open("test1.txt", "w", encoding="UTF-8")
file.write("hello")
file.close()
# 清空文件（文件不存在创建），内容追加
file = open("test1.txt", "a", encoding="UTF-8")
file.write("world")
file.close()
time.sleep(3)
try:
    file = open("test1.txt", "r", encoding="UTF-8")
    print(f"读取到内容5：{file.read()}")
    file.close()
except FileNotFoundError as e:
    print("文件不存在")
    print(e)
finally:
    print("文件操作结束")